<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<link href="/css/common.css" rel="stylesheet" type="text/css" />
<style>
.wrapContent {height:600px;margin:0 auto;padding:0 30px;}
.center {text-align:center;}
.block {padding:10px 30px;}
.lineHeight{height:34px;line-height:34px;}
.right{text-align:right;width:120px;vertical-align:top}
.left {float:left;}
#resultTable .topBorder {border-top:1px solid #000;}
#resultTable .rightBorder {border-right:1px solid #000;}
#resultTable .lineHeight{height:26px;line-height:26px;}
#noResult {display:none;text-align:center;margin:50px 80px;border:1px solid black;padding:20px 0;"}
#page a {margin:auto 4px;}
</style>
</head>
<body>
<div class="wrapContent">
	<div class="block">
		<h2 class="center">订单查询</h2>
		<table id="queryTable" cellpadding="0" cellspacing="0" style="width:100%;">
			<tr class="lineHeight">
				<td class="right">订单号：</td>
				<td><input id="orderSN" style="width:200px;"/></td>
			</tr>
			<tr class="lineHeight">
				<td class="right">机器序号：</td>
				<td><input id="machineSN" style="width:200px;"/></td>
			</tr>
			<tr class="lineHeight">
				<td class="right">下单人：</td>
				<td><input id="orderMan" style="width:200px;"/></td>
			</tr>
			<tr class="lineHeight">
				<td class="right">客户信息：</td>
				<td><input id="customerInfo" style="width:500px;"/></td>
			</tr>
			<tr class="lineHeight">
				<td class="right">订单状态：</td>
				<td>
					<input id="orderType_0" type="radio" name="orderType" value="99" checked/><label for="orderType_0">全部状态</label>
					<input id="orderType_1" type="radio" name="orderType" value="0"/><label for="orderType_1">待发货</label>
					<input id="orderType_2" type="radio" name="orderType" value="1"/><label for="orderType_2">部分发货</label>
					<input id="orderType_3" type="radio" name="orderType" value="2"/><label for="orderType_3">已完成</label>
				</td>
			</tr>
			<tr class="lineHeight">
				<td class="right">下单时间：</td>
				<td><input id="orderTimeBegin" style="width:200px;"/> - <input id="orderTimeEnd" style="width:200px;"/> 如：20120910 - 20120920</td>
			</tr>
			<?php if ($userInfo['level'] > 1000){ ?>
			<tr class="lineHeight">
				<td class="right">发货时间：</td>
				<td><input id="shippedTimeBegin" style="width:200px;"/> - <input id="shippedTimeEnd" style="width:200px;"/> 如：20120910 - 20120920</td>
			</tr>
			<?php } ?>
		</table>
		<div class="center">
			<input id="submit" type="button" value="查询" class="submitBtn"/>
			<input id="export" type="button" value="导出查询结果" class="submitBtn" disabled/> (一次最多导出1000条数据，超出部分请分多次查询导出)
		</div>
		<div style="line-height:20px;height:20px;clear:both;"></div>
		<div><p class="page" id="page"></p></div>
		<table id="resultTable" cellpadding="0" cellspacing="0" style="width:100%;border:1px solid #000;display:none;">
		</table>
		<div id="noResult">没有查询到匹配条件的数据</div>
	</div>
</div>
</body>
<script src="/js/lib.min.js" type="text/javascript"></script>
<script type="text/javascript">
var userLevel = <?php echo $userInfo['level']; ?>;
var pageSize = <?php echo $pageSize; ?>;
function getSelectRadioByName(name) {
	var types = document.getElementsByName(name);
	for(var i=0, type; type=types[i++];){
		if (type.checked){
			return type.value;
		}
	}
	return null;
}

var orderSNInput = $('#orderSN'),
	machineSNInput = $('#machineSN'),
	orderManInput = $('#orderMan'),
	customerInfoInput = $('#customerInfo'),
	orderTimeBeginInput = $('#orderTimeBegin'),
	orderTimeEndInput = $('#orderTimeEnd'),
	submitBtn = $('#submit'),
	exportBtn = $('#export'),
	noResult = $('#noResult'),
	pageArea = $('#page'),
	queryTable = $('#queryTable'),
	resultTable = $('#resultTable');

exportBtn.attr({onclick:function(){
	var orderSN = $.trim(orderSNInput.val()),
		machineSN = $.trim(machineSNInput.val()),
		orderMan = $.trim(orderManInput.val()),
		orderTimeBegin = $.trim(orderTimeBeginInput.val()),
		orderTimeEnd = $.trim(orderTimeEndInput.val()),
		customerName = $.trim(customerInfoInput.val()),
		orderType = getSelectRadioByName('orderType');

	if (userLevel > 1000) {
		var shippedTimeBeginInput = $('#shippedTimeBegin'),
		shippedTimeEndInput = $('#shippedTimeEnd'),
		shippedTimeBegin = $.trim(shippedTimeBeginInput.val()||''),
		shippedTimeEnd = $.trim(shippedTimeEndInput.val()||'');
	}
	
	var data={'orderSN':orderSN||'', 
			'machineSN':machineSN||'', 
			'orderMan':orderMan||'', 
			'orderTimeBegin':orderTimeBegin||'', 
			'orderTimeEnd':orderTimeEnd||'', 
			'shippedTimeBegin':shippedTimeBegin||'', 
			'shippedTimeEnd':shippedTimeEnd||'', 
			'customerName':customerName||'', 
			'orderType':orderType||''};
	
	var param = '';
	$.each(data, function(item, idx){
		param += '&'+idx+'='+item;
	});
	window.open('/admin/order.php?op=doExport&'+param);
}});

submitBtn.attr({onclick:function(){
	doQuery(1);
}});

function doQuery(page) {
	var orderSN = $.trim(orderSNInput.val()),
		machineSN = $.trim(machineSNInput.val()),
		orderMan = $.trim(orderManInput.val()),
		orderTimeBegin = $.trim(orderTimeBeginInput.val()),
		orderTimeEnd = $.trim(orderTimeEndInput.val()),
		customerName = $.trim(customerInfoInput.val()),
		orderType = getSelectRadioByName('orderType');
		
	if (userLevel > 1000) {
		var shippedTimeBeginInput = $('#shippedTimeBegin'),
		shippedTimeEndInput = $('#shippedTimeEnd'),
		shippedTimeBegin = $.trim(shippedTimeBeginInput.val()||''),
		shippedTimeEnd = $.trim(shippedTimeEndInput.val()||'');
	}
	
	submitBtn.val('正在处理，请稍后...');
	submitBtn[0].disabled = true;
	
	$.ajax({
		url:'/admin/order.php?op=doQuery',
		data:{'orderSN':orderSN||'', 
			'machineSN':machineSN||'', 
			'orderMan':orderMan||'', 
			'orderTimeBegin':orderTimeBegin||'', 
			'orderTimeEnd':orderTimeEnd||'', 
			'shippedTimeBegin':shippedTimeBegin||'', 
			'shippedTimeEnd':shippedTimeEnd||'', 
			'customerName':customerName||'', 
			'orderType':orderType||'',
			'page':page},
		type:'POST',
		callback:function(res){
			submitBtn.val('查询')
			submitBtn[0].disabled = false;
			exportBtn[0].disabled = false;
			
			if (res.errno == "0"){
				if (res.data && res.data.length) {
					noResult.hide();
					var pageHtml = '页码：', totalPage = Math.ceil(res.total/pageSize);
					for(var p=1; p<=totalPage; p++) {
						if (page==p) {
							pageHtml += '<a>'+p+'</a>';
						}else {
							pageHtml += '<a href=# onclick="doQuery('+p+');return false;">'+p+'</a>';
						}
					}
					pageArea.html(pageHtml).show();
					var html = '<thead class="lineHeight"><td width="20%" class="rightBorder center">订单号</td><td width="25%" class="rightBorder center">用户信息</td><td width="40%" class="rightBorder center">硒鼓信息</td><td width="15%" class="rightBorder center">下单时间</td></thead>';
					$.each(res.data, function(item, idx){
						html += '<tr class="lineHeight"><td class="topBorder rightBorder center"><a href="/admin/order.php?op=detail&id='+item.id+'">'+item.sn+'</a></td><td class="topBorder rightBorder center">'+item.customer+'</td><td class="topBorder rightBorder center">'+item.cartridges+'</td><td class="topBorder center">'+item.time+'</td></tr>';
					});
					resultTable.html(html).css({display:''});
				}else {
					pageArea.hide();
					resultTable.hide();
					noResult.show();
				}
			}else {
				exportBtn[0].disabled = true;
				alert(res.errmsg);
			}
		}
	});
}
</script>
</html>